package com.avantis.admin.controller;

import com.avantis.admin.config.shiro.CurrentUser;
import com.avantis.admin.service.CustomerService;
import com.avantis.admin.service.SysSerialNumberService;
import com.avantis.admin.service.LevelService;
import com.avantis.common.base.BaseController;
import com.avantis.common.entity.AdminUser;
import com.avantis.common.entity.Customer;
import com.avantis.common.entity.Level;
import com.avantis.common.entity.query.CustomerQuery;
import com.avantis.common.entity.query.LevelQuery;
import com.avantis.common.msg.YResult;
import com.avantis.common.utils.Constants;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Description:    java类作用描述
* @Author:         kirago
* @CreateDate:     2019/1/14 4:14 PM
* @UpdateRemark:   修改内容
* @Version:        1.0
*/

@RestController
@RequestMapping("/customer")
public class CustomerController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomerController.class);

    @Autowired
    private CustomerService customerService;

    @Autowired
    private SysSerialNumberService sysSerialNumberService;

    @Autowired
    private LevelService levelService;


    @PostMapping("/list")
    @ApiOperation(value = "客户列表", httpMethod = "POST", notes = "")
    public YResult list(@RequestBody CustomerQuery customerQuery) {
        YResult yResult = restProcessor(() -> {
            try {
                Map<String,Object> resultMap = new HashMap<>();
                resultMap.put("count",customerService.findCustomerCriteria(customerQuery).getTotalElements());
                resultMap.put("pageCount",customerService.findCustomerCriteria(customerQuery).getTotalPages());
                resultMap.put("models",customerService.findCustomerCriteria(customerQuery).getContent());
                resultMap.put("pageSize",customerService.findCustomerCriteria(customerQuery).getSize());
                resultMap.put("pageIndex",customerService.findCustomerCriteria(customerQuery).getNumber()+1);
                return YResult.ok(resultMap);
            } catch (Exception e) {
                return YResult.error(e.getMessage());
            }
        });
        return yResult;
    }

    @PostMapping("/insert")
    @ApiOperation(value = "添加客户信息", httpMethod = "POST", notes = "")
    public YResult insert(@RequestBody @Validated Customer customer) {
        YResult yResult = restProcessor(() -> {
                AdminUser adminUser = CurrentUser.get();
                customer.setCreater(adminUser.getUsername());
                customer.setModifier(adminUser.getUsername());
                customer.setCreatedon(new Date());
                customer.setModifyon(new Date());
                customer.setCustomerCode(sysSerialNumberService.generateSerialNumberByModelCode(Constants.CUSTOMER_PRE));
                customerService.saveCustomer(customer);
                return YResult.ok();
        });
        return yResult;
    }

    @GetMapping("/detail/{customerId}")
    @ApiOperation(value = "客户信息详情", httpMethod = "GET", notes = "")
    @ApiImplicitParams(@ApiImplicitParam(name = "customerId", value = "客户ID", dataType = "int"))
    public YResult detail(@PathVariable("customerId") String customerId) {
        YResult yResult = restProcessor(() -> {
            try {
                Map<String,Object> resultMap = new HashMap<>();
                resultMap.put("customer",customerService.findOne(customerId));
                return YResult.ok(resultMap);
            } catch (Exception e) {
                return YResult.error(e.getMessage());
            }
        });
        return yResult;
    }

    @DeleteMapping("/delete/{customerId}")
    @ApiOperation(value = "删除客户信息", httpMethod = "GET", notes = "")
    @ApiImplicitParams(@ApiImplicitParam(name = "customerId", value = "客户ID", dataType = "int"))
    public YResult delete(@PathVariable("customerId") String customerId) {
        YResult yResult = restProcessor(() -> {
            try {
                customerService.deleteCustomer(customerId);
                return YResult.ok();
            } catch (Exception e) {
                return YResult.error(e.getMessage());
            }
        });
        return yResult;
    }

    @PostMapping("/modify")
    @ApiOperation(value = "修改客户信息", httpMethod = "POST", notes = "")
    public YResult modify(@RequestBody @Validated Customer customer) {
        YResult yResult = restProcessor(() -> {
            //Customer detail = customerService.findOne(customer.getId());
            AdminUser adminUser = CurrentUser.get();
            customer.setModifier(adminUser.getUsername());
            customer.setModifyon(new Date());
            customerService.modifyCustomer(customer);
            return YResult.ok();
        });
        return yResult;
    }

    @PostMapping("/level/list")
    @ApiOperation(value = "客户等级列表", httpMethod = "POST", notes = "")
    public YResult clevellist(@RequestBody LevelQuery levelQuery) {
        YResult yResult = restProcessor(() -> {
            try {
                Map<String,Object> resultMap = new HashMap<>();
                resultMap.put("count",levelService.findLevelCriteria(levelQuery).getTotalElements());
                resultMap.put("pageCount",levelService.findLevelCriteria(levelQuery).getTotalPages());
                resultMap.put("models",levelService.findLevelCriteria(levelQuery).getContent());
                resultMap.put("pageSize",levelService.findLevelCriteria(levelQuery).getSize());
                resultMap.put("pageIndex",levelService.findLevelCriteria(levelQuery).getNumber()+1);
                return YResult.ok(resultMap);
            } catch (Exception e) {
                return YResult.error(e.getMessage());
            }
        });
        return yResult;
    }

    @PostMapping("/level/insert")
    @ApiOperation(value = "添加客户等级", httpMethod = "POST",notes = "")
    public YResult klevelinsert(@RequestBody @Validated Level level) {
        YResult yResult = restProcessor(() -> {
            if(level.getLevelType() == 0) {
                AdminUser adminUser = CurrentUser.get();
                level.setCreater(adminUser.getUsername());
                level.setModifier(adminUser.getUsername());
                level.setCreatedon(new Date());
                level.setModifyon(new Date());
                levelService.saveLevel(level);
                return YResult.ok();
            } else {
                return YResult.error("新增客户等级失败！");
            }
        });
        return yResult;
    }

    @PostMapping("/level/modify")
    @ApiOperation(value = "修改客户等级信息", httpMethod = "POST", notes = "")
    public YResult clevelmodify(@RequestBody @Validated Level level) {
        YResult yResult = restProcessor(() -> {
            AdminUser adminUser = CurrentUser.get();
            level.setModifier(adminUser.getUsername());
            level.setModifyon(new Date());
            levelService.modifyLevel(level);
            return YResult.ok();
        });
        return yResult;
    }

    @DeleteMapping("/level/delete/{levelId}")
    @ApiOperation(value = "删除客户等级信息", httpMethod = "DELETE", notes = "")
    @ApiImplicitParams(@ApiImplicitParam(name = "levelId", value = "客户等级ID", dataType = "string"))
    public YResult cleveldelete(@PathVariable("levelId") String levelId) {
        YResult yResult = restProcessor(() -> {
            try {
                levelService.deleteLevel(levelId);
                return YResult.ok();
            } catch (Exception e) {
                return YResult.error(e.getMessage());
            }
        });
        return yResult;
    }

    @GetMapping("/level/list/{levelType}")
    @ApiOperation(value = "客户等级列表非分页", httpMethod = "GET", notes = "")
    @ApiImplicitParams(@ApiImplicitParam(name = "levelType", value = "客户等级类型", dataType = "int"))
    public YResult cleveltypelist(@PathVariable("levelType") Integer levelType){
        YResult yResult = restProcessor(() -> {
            try {
                Map<String,Object> resultMap = new HashMap<>();
                resultMap.put("models", levelService.findCustomerLevels(levelType));
                return YResult.ok(resultMap);
            } catch (Exception e) {
                return YResult.error(e.getMessage());
            }
        });
        return yResult;
    }

    @PostMapping("/import")
    @ApiOperation(value = "导入客户信息", httpMethod = "POST", notes = "")
    public YResult importCustomer(MultipartFile multipartFile) {
        YResult yResult = restProcessor(() -> {

            return YResult.ok();
        });
        return yResult;
    }

}
